import { _decorator, Component, EditBox, Node } from 'cc';
import { UIManager } from './UIManager';
import GameRpc from '../api/GameRpc';
const { ccclass, property } = _decorator;

@ccclass('Start')
export class Start extends Component {

    @property(EditBox)
    editBox:EditBox
    @property(Node)
    submit:Node

    start() {

        this.submit.on(Node.EventType.TOUCH_END, this.onSubmit, this)


        let id = this.getQuery().id
        let token = this.getQuery().token
        if(id){
           
            GameRpc.setOpenid(id)
            UIManager.instance.loadScene('village')
        }
        
        if(token){
            GameRpc.setToken(token)
        }
        
    }

    onSubmit() {
        let openid = this.editBox.string.trim()
       

        GameRpc.setOpenid(openid)

        UIManager.instance.loadScene('village')
    }

    getQuery() {
        let list = location.search.substring(1).split('&')
        let obj = { id: '', token: '' }
        for (let item of list) {
            let arr = item.split('=')
            obj[arr[0]] = arr[1]
        }
        return obj
    }
}


